package database;
/**

 * @author 
 */

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import jcolibri.util.FileIO;
import org.hsqldb.Server;

public class HSQLDBserver {
    static boolean initialized = false;

    private static Server server;

	@SuppressWarnings("unchecked")
	public static void init(){
		if (initialized)
		    return;
	    org.apache.commons.logging.LogFactory.getLog(HSQLDBserver.class).info("Creating data base ...");
	
		server = new Server();
		server.setDatabaseName(0, "matricula");
		server.setDatabasePath(0, "mem:matricula;sql.enforce_strict_size=true");
			
		server.setLogWriter(null);
		server.setErrWriter(null);
		server.setSilent(true);
		server.start();
	
		initialized = true;
		try
		{
		    Class.forName("org.hsqldb.jdbcDriver");
	
		    PrintStream out = new PrintStream(new ByteArrayOutputStream());
		    Connection conn = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/matricula", "sa", "");
		    SqlFile file=SqlFile.getInstancia(new File(FileIO.findFile("etc/matricula.sql").getFile()),false,new HashMap());
		    System.out.println("RUTA: "+FileIO.findFile("etc/matricula.sql").getFile());
		    file.execute(conn,out,out, true);
		    org.apache.commons.logging.LogFactory.getLog(HSQLDBserver.class).info("Data base generation finished");
		    
		} catch (Exception e)
		{ e.printStackTrace();
		    org.apache.commons.logging.LogFactory.getLog(HSQLDBserver.class).error(e);
		}

    }

    public static void shutDown(){
		if (initialized){
		    server.stop();
		    initialized = false;
		}
    }

    /*public static void main(String[] args)
    {
	HSQLDBserver.init();
	HSQLDBserver.shutDown();
	System.exit(0);
	
    }*/

}
